/**
 * @Author：ming
 * @Date：2023/9/26 2:38 PM
 * @Filename：Lc2
 */

/**
 * @author ming
 */
public class Lc2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if(l1==null){
            return l2;
        }else if(l2==null){
            return l1;
        }

        int x ,y;
        int res = 0;
        ListNode head = new ListNode();
        head.next =null;
        ListNode p = head;


        while (l1!=null||l2!=null){
            x = l1==null?0:l1.val;
            y= l2==null?0:l2.val;

            int sum = (x+y+res)%10;
            res = (x+y+res)/10;

            ListNode q = new ListNode(sum);
            p.next =q;
            p=q;

            if(l1!=null){
                l1 =l1.next;
            }
            if(l2!=null){
                l2=l2.next;
            }
        }

        if(res!=0){
            ListNode q = new ListNode(res);
            p.next = q;
            p=q;
        }
        return head.next;
    }
}
